<template>
  <div class="flex flex-col h-full">
    <SearchContainer @reset="() => formRef.resetFields()" @search="queryList">
      <a-form ref="formRef" :model="formState">
        <a-form-item name="ks" class="w-1/4">
          <a-input v-model:value="formState.zjm" placeholder="支付名称/支付代码" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <BaseTable
        column-code="00001124"
        fill
        :data-source="tableData"
        bordered
        :pagination="{ total: page.total, current: page.current, pageSize: page.pageSize }"
        @change="onPageChange"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.dataIndex === 'isuse'">
            <a-tag v-if="!record.isuse" color="green">启用</a-tag>
            <a-tag v-else color="red">停用</a-tag>
          </template>
          <template v-if="column.dataIndex === 'issystem'">
            <span v-if="!record.issystem">是</span>
            <span v-else>否</span>
          </template>
          <template v-if="column.dataIndex == 'action'">
            <a-button type="link" size="small" :disabled="!record.issystem" @click="onClickEdit(record)">修改</a-button>
            <!-- <a-divider type="vertical" />
            <ConfirmModal description="确定删除?" @confirm="onClickDelete(record.id)">
              <a-button type="text" danger size="small"> 删除 </a-button>
            </ConfirmModal> -->
            <a-divider type="vertical" />
            <a-button type="link" size="small" :disabled="!record.isuse" @click="onClickState(record)">启用</a-button>
            <a-divider type="vertical" />
            <ConfirmModal description="确定停用?" @confirm="onClickState(record)">
              <a-button type="text" danger :disabled="record.isuse" size="small"> 停用 </a-button>
            </ConfirmModal>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="queryList" @confirm="editOpen = false" />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import ConfirmModal from '@/components/ConfirmModal'
import { post } from '@/utils/request'
import Edit from './Edit'

const formRef = ref(null)
const editOpen = ref(false)
const loading = ref(false)
const formState = ref({})
const tableData = ref([])
const rowData = ref({})
const page = ref({ total: 0, current: 1, pageSize: getSystemConfig('DFFAULT_PAGE_SHOW_COUNT') || 10 })
const queryList = async () => {
  loading.value = true
  const { code, data, total, msg } = await post('/operation/opex/getPaymentTypeInfo', {
    zjm: formState.value.zjm,
    pageNum: page.value.current,
    pageSize: page.value.pageSize
  })
  loading.value = false
  if (code !== 0) return message.error(msg)
  tableData.value = data
  page.value.total = total
}
// const onClickDelete = async (id) => {
//   const { code, msg } = await post('/operation/opex/doDeletePaymentType', { id })
//   if (code !== 0) return message.error(msg)
//   message.success(msg)
//   queryList()
// }
const onClickState = async (row) => {
  const { code, msg } = await post('/operation/opex/doUpdatePaymentType', { id: row.id, isuse: row.isuse })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  queryList()
}

const onClickEdit = (row) => {
  rowData.value = { ...row }
  editOpen.value = true
}

const onPageChange = ({ current, pageSize }) => {
  page.value.current = current
  page.value.pageSize = pageSize
  queryList()
}

onMounted(() => {
  queryList()
})
</script>
